package com.example.taigame;

import java.io.File;
import java.util.ArrayList;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Looper;
import android.util.Log;
import android.view.View;

import com.example.model.GameModel;
import com.example.model.ListGameType;
import com.example.utils.Utils;
import com.google.android.gcm.GCMRegistrar;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class MyApp extends Application {

	public static BaseActivity _currentActivity;
	public static ArrayList<ListGameType> listGameType = new ArrayList<ListGameType>();

	static MyApp _instance;
	public static GameModel currentSelectedGame;
	static final String DISPLAY_MESSAGE_ACTION =
            "com.example.taigame.DISPLAY_MESSAGE";

	public static void initListGameType() {
		for (int i = 0; i < Utils.gameType.length; i++) {
			ListGameType gameType = new ListGameType(Utils.gameName[i],
					String.valueOf(Utils.gameType[i]), 0, 30, null);
			listGameType.add(gameType);
		}
	}

	public static MyApp getInstance() {
		return _instance;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		_instance = this;

		File cacheDir = StorageUtils.getCacheDirectory(this);
		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.m_0_image_loading)
				// resource or drawable
				.showImageForEmptyUri(R.drawable.m_0_image_loading)
				// resource or drawable
				.showImageOnFail(R.drawable.m_0_image_loading)
				// resource or drawable
				.imageScaleType(ImageScaleType.EXACTLY)
				.displayer(new FadeInBitmapDisplayer(2000))
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				this).diskCacheExtraOptions(480, 320, null)

		.threadPoolSize(5).memoryCache(new LruMemoryCache(2 * 1024 * 1024))
				.diskCache(new UnlimitedDiscCache(cacheDir)) // default
				.defaultDisplayImageOptions(defaultOptions)
				// .writeDebugLogs()
				.build();
		ImageLoader.getInstance().init(config);
		
		
		// GCM REG
		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);
		registerReceiver(receiver, new IntentFilter(
                this.DISPLAY_MESSAGE_ACTION));
		final String regId = GCMRegistrar.getRegistrationId(this);
		if (regId.equals("")) {         
            GCMRegistrar.register(this, Utils.GCM_ID);
        } else{
        	Log.w("GCMIntentService",regId);
        }
	}
	
	private BroadcastReceiver receiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			
			Log.w("GCMIntentService","Message Reciever");
		}
	};

	public static void showProgressDialog(final String text,
			final boolean isShowAnimation) {
		if (_currentActivity == null)
			return;
		if (Looper.myLooper() == Looper.getMainLooper()) {
			_currentActivity.showProgressDialog(text, isShowAnimation);
		} else {
			_currentActivity.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					_currentActivity.showProgressDialog(text, isShowAnimation);
				}
			});
		}

	}

	public static void hideProgressDialog() {
		if (_currentActivity == null)
			return;
		_currentActivity.hideProgressDialog();
	}

	public static void showOKDialog(final String text) {
		showOKDialog(text, null);
	}

	public static void showOKDialog(final String text,
			final View.OnClickListener okClick) {
		if (_currentActivity == null)
			return;
		if (Looper.myLooper() == Looper.getMainLooper()) {
			_currentActivity.showOkDialog(text, okClick);
		} else {
			_currentActivity.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					_currentActivity.showOkDialog(text, okClick);
				}
			});
		}
	}
}
